package reflect;

import java.lang.reflect.Constructor;

/**
 * 使用指定的构造器实例化对象
 */
public class ReflectDemo3 {
    public static void main(String[] args) throws Exception{
        //静态绑定
        /*
        Person p = new Person("张三",23);
        System.out.println(p);
         */

        //动态绑定
        Class cls = Class.forName("reflect.Person");//获取Person的类对象
        /*
         * Constructor 是反射对象之一, 它的每个实例用于表示一个构造器对象
         */
        Constructor constructor = cls.getConstructor();//获取String无参构造器
        Object obj = constructor.newInstance();//new Person();
        System.out.println(obj);

        Constructor constructor1 = cls.getConstructor(String.class);//获取一个参, String参构造器
        Object obj1 = constructor1.newInstance("张三"); //new Person("张三");
        System.out.println(obj1);

        Constructor constructor2 = cls.getConstructor(String.class,int.class);//获取两个参, String,int参构造器
        Object obj2 = constructor2.newInstance("李四",23);//new Person("李四",23);
        System.out.println(obj2);

    }
}
